package com.hjx.movie.core.system.controller.admin;

import com.hjx.movie.common.utils.Result;
import com.hjx.movie.common.utils.ResultType;
import com.hjx.movie.common.utils.tree.TreeBuilder;
import com.hjx.movie.common.utils.tree.TreeNode;
import com.hjx.movie.core.system.service.admin.IMenuService;
import com.hjx.movie.core.system.service.admin.IRoleMenuService;
import com.hjx.movie.core.system.vo.MenuTreeNode;
import com.hjx.movie.core.system.vo.CheckedIdsVO;
import com.hjx.movie.persistence.system.model.IvyMenu;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2021/1/19 - 21:30
 */
@RestController
@Slf4j
@RequestMapping("/menu")
public class MenuController {
    @Resource
    IMenuService menuService;
    @Resource
    IRoleMenuService roleMenuService;
    @GetMapping("/tree")
    public Result findMenuTree(@RequestParam(name = "rootMenuId") Long rootMenuId,
                              @RequestParam(name = "menuNameLike",required = false)String menuNameLike,
                              @RequestParam(name = "menuStatus",required = false)Boolean menuStatus){
        log.debug("findMenuTree接受到的入参===> rootMenuId:{},menuNameLike:{},menuStatus:{}", rootMenuId, menuNameLike, menuStatus);
        if (StringUtils.isNotBlank(menuNameLike)|| menuStatus !=null){
            List<MenuTreeNode> list =  menuService.selectMenuTree(rootMenuId, menuNameLike, menuStatus);
            list.sort(Comparator.comparing(TreeNode::getNodeId));
            return Result.success(list);
        }
        return Result.success(TreeBuilder.treeSort(menuService.selectMenuTree(rootMenuId, menuNameLike, menuStatus)));
    }

    @GetMapping("/findLeftMenuAndTabs")
    public Result findLeftMenuAntTabs(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        log.debug("findMenuTree接受到的入参===> userId:{}",authentication.getName());
        List<MenuTreeNode> menuTreeNodes = TreeBuilder.treeSort(menuService.findLeftMenuAndTabs(authentication.getName()));
        List<MenuTreeNode> nodes = new ArrayList<>(menuTreeNodes);
        Map<String,Object> map = new HashMap<>();
        int index = -1;
        for (int i=0;i<menuTreeNodes.size();i++){
            if (menuTreeNodes.get(i).getMenuType().equals("T")){
                index = i;
                break;
            }
        }
        if (index!=-1){
            menuTreeNodes.remove(index);
        }

        map.put("menuList",menuTreeNodes);
        map.put("tabs",nodes);
        return Result.success(map);

    }



    @PostMapping("/add")
    public Result addMenu(@RequestBody IvyMenu ivyMenu){
        log.debug("addMenu接收到的入参===>:{}",ivyMenu);
        if(menuService.addMenu(ivyMenu)){
            return Result.success("新增菜单成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR);
    }

    @GetMapping("/delete")
    public Result deleteMenu(@RequestParam("menuId")Long menuId){
        log.debug("deleteMenu接收到的入参===>:{}",menuId);
        if (menuService.deleteMenu(menuId)){
            return Result.success("删除菜单成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR);
    }

    @PostMapping("/update")
    public Result updateMenu(@RequestBody IvyMenu menu){
        log.debug("updateMenu接收到的入参===>:{}",menu);
        if (menuService.updateMenu(menu)){
            return Result.success("修改菜单信息成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR);
    }

    @GetMapping("/findCheckedTree")
    public Result findMenuCheckedKeys(@RequestParam("roleId") Long roleId){
        log.debug("接收到的入参===>:{}",roleId);
        Map<String,Object> map = new HashMap<>();
        map.put("tree",menuService.selectMenuTree(1L, null,null));
        map.put("expandedKeys",menuService.findMenuExpandedKeys());
        map.put("checkedKeys",menuService.findMenuCheckedKeys(roleId));
        return Result.success(map);
    }

    @PostMapping("/addOrDeleteRoleMenu")
    public Result addRoleMenu(@RequestBody CheckedIdsVO checkedIdsVO){
        log.debug("接受到的入参为:{}", checkedIdsVO);
        if (roleMenuService.addOrDeleteMenuBatch(checkedIdsVO.getId(), checkedIdsVO.getCheckedIds())){
            return Result.success("保存菜单权限成功!");
        }
        return Result.error(ResultType.USER_INPUT_ERROR,"保存菜单权限失败!");
    }
}
